//
//  FTPresenter.m
//  FTPicker
//
//  Created by JackieYip on 2017/6/9.
//  Copyright © 2017年 mondial-d. All rights reserved.
//

#import "FTPresenter.h"

#import "FTMacro_Screen.h"

#define kTitleHeight        30.0f
#define kMaxContentHeight   440.0f
#define kMinContentHeight   0.0f

@interface FTPresenter ()
@property (nonatomic, weak) id presenterDelegate;
@property (nonatomic) UIButton *maskView;
@property (nonatomic) UILabel *titleLabel;
@property (nonatomic, readwrite) BOOL visible;
@property (assign, nonatomic, readwrite) BOOL isWithoutMaskView;
@end

@implementation FTPresenter
@dynamic delegate;

- (instancetype)init
{
    if (self = [super init]) {
        self.maskView = [[UIButton alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.maskView.backgroundColor = [UIColor clearColor];
        [self.maskView addTarget:self action:@selector(onMaskClicked) forControlEvents:UIControlEventTouchUpInside];
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = [UIColor darkGrayColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        self.titleLabel.backgroundColor = [UIColor colorWithWhite:238.0f/255 alpha:1.0f];
        self.containerView = [[UIView alloc] init];
        self.containerView.backgroundColor = [UIColor whiteColor];
        self.minContentHeight = kMinContentHeight;
        self.maxContentHeight = kMaxContentHeight;
    }
    
    return self;
}

- (instancetype)initWithoutMaskView
{
    if (self = [super init]) {
        self.isWithoutMaskView = YES;
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = [UIColor darkGrayColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        self.titleLabel.backgroundColor = [UIColor colorWithWhite:238.0f/255 alpha:1.0f];
        self.containerView = [[UIView alloc] init];
        self.containerView.backgroundColor = [UIColor whiteColor];
        self.minContentHeight = kMinContentHeight;
        self.maxContentHeight = kMaxContentHeight;
    }
    
    return self;
}

- (id<FTPresenterDelegate>)delegate {
    return self.presenterDelegate;
}

- (void)setDelegate:(id<FTPresenterDelegate>)delegate
{
    self.presenterDelegate = delegate;
}

- (void)onMaskClicked
{
    [self dismiss];
}

- (void)dismiss
{
    if (!self.visible)
        return;
    
    if ([self.presenterDelegate respondsToSelector:@selector(presenterWillDismiss:)]) {
        [self.presenterDelegate presenterWillDismiss:self];
    }
    
    [self dismissOnCompletion:^{
        self.visible = NO;
        if ([self.presenterDelegate respondsToSelector:@selector(presenterDidDismiss:)]) {
            [self.presenterDelegate presenterDidDismiss:self];
        }
    }];
}

- (void)dismissOnCompletion:(void (^)(void))completion
{
    CGRect containerRect = self.containerView.frame;
    containerRect = CGRectOffset(containerRect, 0.0f, containerRect.size.height);
    [UIView animateWithDuration:0.3f animations:^{
        self.maskView.backgroundColor = [UIColor clearColor];
        self.containerView.frame = containerRect;
    } completion:^(BOOL finished) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion)
                completion();
        });
        if (!_isWithoutMaskView)
            [self.maskView removeFromSuperview];
        else
            [self.containerView removeFromSuperview];
    }];
}

- (void)present
{
    if (self.visible)
        return;
    self.visible = YES;
    
    for (UIView *subView in self.containerView.subviews) {
        [subView removeFromSuperview];
    }
    
    NSString *title = nil;
    if ([self.presenterDelegate respondsToSelector:@selector(titleForPresenter:)])
        title = [self.presenterDelegate titleForPresenter:self];
    self.titleLabel.text = title;
    [self.containerView addSubview:self.titleLabel];
    
    UIView *contentView = nil;
    if ([self.presenterDelegate respondsToSelector:@selector(contentViewInPresenter:)])
        contentView = [self.presenterDelegate contentViewInPresenter:self];
    if ([contentView isKindOfClass:[UIView class]]) {
        self.containerView.backgroundColor = [UIColor clearColor];
        [self.containerView addSubview:contentView];
    }
    
    CGRect titleRect;
    if (!_isWithoutMaskView)
        titleRect = self.maskView.bounds;
    else
        titleRect = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    titleRect.origin.y = 0.0f;
    titleRect.size.height = title.length > 0 ? kTitleHeight : 0.0f;
    self.titleLabel.frame = titleRect;
    
    CGRect contentRect = CGRectOffset(titleRect, 0.0f, titleRect.size.height);
    contentRect.size.height = contentView.frame.size.height;
    if (contentRect.size.height > self.maxContentHeight)
        contentRect.size.height = self.maxContentHeight;
    if (contentRect.size.height < self.minContentHeight)
        contentRect.size.height = self.minContentHeight;
    if (contentView)
        contentView.frame = contentRect;
    
    CGRect containerRect = titleRect;
    if (!_isWithoutMaskView)
        containerRect.origin.y = self.maskView.bounds.size.height;
    else
        containerRect.origin.y = kScreenHeight;
    containerRect.size.height = titleRect.size.height + contentRect.size.height;
    self.containerView.frame = containerRect;
    if (!_isWithoutMaskView)
        [self.maskView addSubview:self.containerView];
    
    if (contentView) {
        contentView.translatesAutoresizingMaskIntoConstraints = NO;
        NSDictionary *metrics = @{@"titleLabelHeight": @(titleRect.size.height)};
        NSDictionary *views = @{@"contentView":contentView, @"titleLabel":self.titleLabel};
        NSArray *constraintsH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[contentView]-0-|" options:0 metrics:nil views:views];
        NSArray *constraintsV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-titleLabelHeight-[contentView]-0-|" options:0 metrics:metrics views:views];
        [self.containerView addConstraints:constraintsH];
        [self.containerView addConstraints:constraintsV];
    }
    
    containerRect = CGRectOffset(containerRect, 0.0f, -containerRect.size.height);
    [UIView animateWithDuration:0.3f animations:^{
        self.maskView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:0.8f];
        self.containerView.frame = containerRect;
    }];
    
    if (!_isWithoutMaskView)
        [[UIApplication sharedApplication].windows.firstObject addSubview:self.maskView];
    else
        [[UIApplication sharedApplication].windows.firstObject addSubview:self.containerView];
}

@end
